package com.laiyima.zhongjiang.linghuilv.demo.home;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.laiyima.zhongjiang.linghuilv.demo.R;
import com.laiyima.zhongjiang.linghuilv.demo.adapter.TaxbeanAdapter;
import com.laiyima.zhongjiang.linghuilv.demo.bean.Taxbean;

import org.json.JSONArray;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public class Tax_Instead_Activity extends SwipeBackActivity implements View.OnClickListener {
    private String TAG="Tax_Instead_Activity";
    private int taxCode;
    private ListView mListView;
    private List<Taxbean> mTaxbeanDatas = new ArrayList<>();
    private TaxbeanAdapter mTaxbeanAdapter;
    private Taxbean mTaxbean;
    private LinearLayout mImageView;
    private TextView mTextView;
    private LinearLayout linearLayout;


    protected boolean useThemestatusBarColor = false;//是否使用特殊的标题栏背景颜色，android5.0以上可以设置状态栏背景色，如果不使用则使用透明色值
    protected void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            //根据上面设置是否对状态栏单独设置颜色
            if (useThemestatusBarColor) {
                // getWindow().setStatusBarColor(getResources().getColor(R.color.colorTheme));
            } else {
                getWindow().setStatusBarColor(Color.TRANSPARENT);
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ) {//android6.0以后可以对状态栏文字颜色和图标进行修改
            getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tax__instead_);
        mListView= (ListView) findViewById(R.id.tax_list);
        mImageView= (LinearLayout) findViewById(R.id.tx_back);
        mTextView= (TextView) findViewById(R.id.add_tax);
        setStatusBar();
        linearLayout= (LinearLayout) findViewById(R.id.show_list_add);

        mImageView.setOnClickListener(this);
        mTextView.setOnClickListener(this);
        linearLayout.setOnClickListener(this);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ListView listView=(ListView)parent;
                Taxbean mTaxbean=(Taxbean)listView.getItemAtPosition(position);
                String taxNo=mTaxbean.getTaxNo();
                String compenyName=mTaxbean.getBillingName();
                Intent Taxlistintent=new Intent(Tax_Instead_Activity.this,TaxlistActivity.class);
                Taxlistintent.putExtra("taxNo", taxNo);
                Taxlistintent.putExtra("compenyName", compenyName);
                startActivity(Taxlistintent);
            }
        });



        intView();
        intLinear();
    }

    @Override
    protected void onResume() {
        super.onResume();
//        int i=mTaxbeanDatas.size();
//        if(i==0){
//            linearLayout.setVisibility(View.VISIBLE);
//        }else {
//            linearLayout.setVisibility(View.GONE);
//        }
        intLinear();
    }

    private void intLinear() {
        SharedPreferences sharedPreferences =getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
        String show = sharedPreferences.getString("listShow", "");
        if(show.equals("listShow")){
            linearLayout.setVisibility(View.VISIBLE);
        }if(show.equals("listNotShow")){
            linearLayout.setVisibility(View.GONE);
        }

    }

    private void intView() {
        SharedPreferences sharedPreferences =getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
        String userID = sharedPreferences.getString("userID", "");
        RequestParams params1 = new RequestParams("http://zljl.laiima.com/index.php?s=/hjs_api/Invoice/count_invoice");
        //需要带入的参数
        params1.addBodyParameter("uid", userID);
        // params.addBodyParameter("captcha", captcha);
        x.http().post(params1, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.e(TAG, "回调成功");
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    Log.e(TAG, "回调成功"+jsonObject);
                    taxCode = jsonObject.optInt("code", 0);
                    //当你获得JSONArray 类型的时候，然后要写for循环遍历获取里面每一个Object
                    JSONArray data = jsonObject.getJSONArray("data");
                    for (int i = 0; i < data.length(); i++) {
                        JSONObject value = data.getJSONObject(i);
                        String count = value.getString("count");
                        String billingName = value.getString("billingName");
                        String count_money = value.getString("count_money");
                        String taxNo = value.getString("taxNo");
                        String taxMobile = value.getString("taxMobile");
                        mTaxbean = new Taxbean(count,billingName,count_money,taxNo,taxMobile);
                        mTaxbeanDatas.add(mTaxbean);
                    }
                    Log.e(TAG, "回调成功result"+result);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (taxCode == 200) {
                    mTaxbeanAdapter = new TaxbeanAdapter(getBaseContext().getApplicationContext(),mTaxbeanDatas);
                    mListView.setAdapter(mTaxbeanAdapter);
                }
                if (taxCode == 500) {
                    Toast.makeText(getBaseContext().getApplicationContext(), "系统异常", Toast.LENGTH_SHORT).show();
                    Log.e(TAG, "系统异常");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
//                        Intent Nameintent=new Intent(HomeActivity.this,NameActivity.class);
//                        startActivity(Nameintent);
                mTaxbeanAdapter = new TaxbeanAdapter(getBaseContext().getApplicationContext(),mTaxbeanDatas);
                mListView.setAdapter(mTaxbeanAdapter);
            }
            @Override
            public void onCancelled(CancelledException cex) {

            }
            @Override
            public void onFinished() {
                intLinear();
            }
        });


    }


    @Override
    public void onClick(View v) {
        int id=v.getId();
        switch (id){
            case R.id.show_list_add:
                intView();
                SharedPreferences sharedPreferences =getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
                String show = sharedPreferences.getString("listShow", "");
                if(show.equals("listShow")){
                    Toast.makeText(getBaseContext().getApplicationContext(), "暂无数据", Toast.LENGTH_SHORT).show();
                    intLinear();
                }else{
                    intLinear();
                    Toast.makeText(getBaseContext().getApplicationContext(), "已刷新数据", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.tx_back:
                 finish();
                break;
            case R.id.add_tax:
                Intent AddTaxintent=new Intent(Tax_Instead_Activity.this,AddTaxActivity.class);
                startActivity(AddTaxintent);
                break;
        }

    }
}
